package com.securitytrace.dataModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.table.DefaultTableModel;

import com.securitytrace.model.Role;
import com.securitytrace.util.ConvertAdapter;
import com.securitytrace.util.RoleConvertToVector;

public class RoleDataModel extends DefaultTableModel {

	private static final long serialVersionUID = -8000339432065418213L;
	private static final String[] roleColumns = new String[] { "名称", "描述" };

	private Map<Integer, Role> roleMap = new HashMap<Integer, Role>();
	private int roleCurrentPage;
	private int roleTotal;
	private static ConvertAdapter<Role> roleConvertToVector;

	@SuppressWarnings("rawtypes")
	private Class[] types = new Class[] { java.lang.String.class,
			java.lang.String.class };
	private boolean[] canEdit = new boolean[] { false, false };

	static {
		roleConvertToVector = new RoleConvertToVector();
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return canEdit[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return types[columnIndex];
	}

	/** 初始化数据 **/
	public void setDataVector(List<Role> roles) {
		for (int row = 0; row < roles.size(); row++) {
			roleMap.put(row, roles.get(row));
		}
		super.setDataVector(roleConvertToVector.convertToVector(roles),
				roleConvertToVector.convertToVector(roleColumns));
	}

	/** 获取一行数据 **/
	public Role getRow(int row) {
		return roleMap.get(row);
	}

	/*** 更新一行数据 **/
	public void setValueAtRow(Role role, int row) {
		roleMap.put(row, role);
		this.setValueAt(role.getName(), row, 0);
		this.setValueAt(role.getDescription(), row, 1);
	}

	/***以下为get/set方法**/
	public Map<Integer, Role> getRoleMap() {
		return roleMap;
	}
	public void setRoleMap(Map<Integer, Role> roleMap) {
		this.roleMap = roleMap;
	}
	public int getRoleTotal() {
		if(roleTotal > 0) {
			return roleTotal;
		}else {
			return 1;
		}
	}
	public void setRoleTotal(int roleTotal) {
		this.roleTotal = roleTotal;
	}
	public int getRoleCurrentPage() {
		if(0 < roleCurrentPage) {
			return roleCurrentPage;
		}else {
			return 1;
		}
	}
	public void setRoleCurrentPage(int roleCurrentPage) {
		this.roleCurrentPage = roleCurrentPage;
	}
	public String getRolePageInfo() {
		return "共 " + this.getRoleTotal() +" 页，当前为第 "+this.getRoleCurrentPage()+" 页";
	}
}
